package com.china08.yxyapi.api.manage.edu.platform.slider;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.china08.yxyapi.api.YxyApiBaseController;
import com.china08.yxyapi.entity.mg.platform.YxyHomeSlider;
import com.china08.yxyapi.model.YxyApiReqModel;
import com.china08.yxyapi.model.YxyApiRespModel;
import com.china08.yxyapi.repository.mg.YxyHomeSliderRepository;

import io.swagger.annotations.ApiOperation;

/**
 * 教育局平台办公 焦点图管理 新增
 * 
 */
@CrossOrigin
@RestController
public class SliderCrtController extends YxyApiBaseController {

    @Autowired
    private YxyHomeSliderRepository yhsr;

    @ApiOperation(value = "教育局 创建焦点图")
    @ResponseBody
    @RequestMapping(value = "/api_yw/manage/edu/platform/slider/crt", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public RespModel precess(HttpServletRequest request, HttpServletResponse response, @RequestBody ReqModel reqModel) {
        RespModel model = new RespModel();
        String eduId = getProfile(request, response).getSchoolId();
        Integer countSlider = yhsr.countByEdbIdAndStatus(eduId, 0);
        Assert.isTrue(countSlider < 5, "轮播图最多5张");
        YxyHomeSlider homeSlider = new YxyHomeSlider();
        homeSlider.setImg(reqModel.img);
        homeSlider.setUrl(reqModel.url);
        homeSlider.setEdbId(eduId);
        homeSlider.setStatus(0);
        YxyHomeSlider maxOrder = yhsr.findTop1ByEdbIdOrderByOrderNumDesc(eduId);
        if (maxOrder != null) {
            homeSlider.setOrderNum(maxOrder.getOrderNum() + 1);
        }
        yhsr.save(homeSlider);
        model.setMessage("成功");
        return model;
    }

    protected static class ReqModel extends YxyApiReqModel {
        public String url;
        public String img;

        @Override
        public void autoCheck() {
            Assert.notNull(StringUtils.defaultIfBlank(img, null), "请上传焦点图");
        }

    }

    protected static class RespModel extends YxyApiRespModel {

    }
}
